<?php 

class indexController extends appController
{
	
	public function index()
	{
	    $this->getWxUser(false);
	    $index = Model('index');
	    $recode = $index->getRecode($_SESSION['wxuser']['id'], $_SESSION['wxuser']['openid']);
	    $type = 1;
	    if($recode)
	    {
	        $type = 0;
	    }
	    
	    $shareInfo = $index->getShareInfo('index');
	    $this->assign('recode', $recode);
	    $this->assign('type', $type);
	    
	    $this->assign('user', $_SESSION['wxuser']);
	    
	    $shareInfo['share_logo'] = 'http://'.$_SERVER['HTTP_HOST'].$shareInfo['share_logo'];
	    $this->assign('shareInfo', $shareInfo);
	    $this->assign('jsdk', $index->get_jsdk());
		$this->assign_global();
		$this->display("index.html");
	}
	
	public function getWxUser($type = false)
	{
	    if(!$_SESSION['wxuser']['id'] && !$_SESSION['wxuser']['openid'])
	    {
	        $wx = new weixinController();
	        $index = Model('index');
	        $config = $index->weixin_config();
	        if(!$_GET['code'])
	        {
	            $scope = $type ? 'user_info' : 'user_base';
	            $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'&type='.$scope;
	            $wx->getUserUrl($config['appid'], $url, $type);
	        }
	        else
	        {
	            $code = $_GET['code'];
	            $scope = $_GET['type'];
	            $arr = $wx->getUserByCode($config['appid'], $config['appsecret'], $code);
	            if($scope == 'user_base')
	            {
    	            $isExistOpenid = $index->isExistOpenid($arr['openid']);
    	            if(!$isExistOpenid)
    	            {
    	                $url = 'http://'.$_SERVER['HTTP_HOST'].'/index.php?g=mobile&m=index&a=index';
    	                $wx->getUserUrl($config['appid'], $url, true);
    	            }
    	            else
    	            {
    	                $userInfo = $index->getUserInfo($arr['openid']);
    	                $_SESSION['wxuser'] = $userInfo;
    	            }
	            }
	            else 
	            {
	                $str = $wx->getUserInfo($arr['openid'], $arr['access_token']);
	                $info['openid'] = $str['openid'];
	                $info['nickname'] = $str['nickname'];
	                $info['sex'] = $str['sex'];
	                $info['headimgurl'] = $str['headimgurl'];
	                $info['country'] = $str['country'];
	                $info['city'] = $str['city'];
	                $province = $info['province'] = $str['province'];
	                if($info['country'] == "中国台湾")
	                {
	                    $province = "台湾";
	                }
	                elseif($info['country'] == "中国香港")
	                {
	                    $province = "香港";
	                }
	                elseif($info['country'] == "中国澳门")
	                {
	                    $province = "澳门";
	                }
	                $info['province_code'] = $index->getProvince($province);
	                $info['city_code'] = $info['province_code'] ? $index->getCity($info['city']) : 0;
	                $info['access_time'] = yf_time() + $arr['expires_in'] - 200;
	                $id = $index->insertWxuser($info);
	                if(!$id)
	                {
	                   die("系统繁忙");
	                }
	                $_SESSION['wxuser'] = $info;
	                $_SESSION['wxuser']['id'] = $id;
	            }
	        }
	    }
	}
	
	public function getCard()
	{
	    $index = Model('index');
	    $res = array('error' => 1, 'content' => '系统繁忙');
        $info = $this->getPrize();
        if($_SESSION['wxuser'])
        {
            if($_SESSION['wxuser']['id'] != $_POST['wxid'] || $_SESSION['wxuser']['openid'] != $_POST['openid'] || $_SESSION['wxuser']['province_code'] != $_POST['province'] || $_SESSION['wxuser']['city_code'] != $_POST['city'])
            {
                $this->showMsg($res);
            }
            $infoTemp = $index->getRecode($_SESSION['wxuser']['id'], $_SESSION['wxuser']['openid']);
            if($infoTemp)
            {
                $infoTemp['card_money'] = $infoTemp['money'];
                $res = array('error' => 0, 'content' => $infoTemp);
                $this->showMsg($res);
            }
            $data['wxid'] = $_SESSION['wxuser']['id'];
            $data['openid'] = $_SESSION['wxuser']['openid'];
            $data['card_id'] = $info['id'];
            $data['money'] = $info['card_money'];
            $data['ip'] = hy_getIP();
            $data['created'] = yf_time();
            $data['province'] = $_SESSION['wxuser']['province_code'];
            $data['city'] = $_SESSION['wxuser']['city_code'];
            $id = $index->insertRecode($data);
            if($id)
            {
                $index->updateCardNum($data['card_id']);
                $res = array('error' => 0, 'content' => $info);
            }
        }
        else 
        {
            $res['content'] = '信息错误,请重新进入该页面';
        } 
        $this->showMsg($res);
	}	
	
	public function getPrize()
	{
	    header('Content-type:text/html;charset=utf-8;');
	    $arr = array();
	    $index = Model('index');
	    $prize = $index->getAllCard();
	    $allCount = $index->getAllCardNum();
	    
	    foreach($prize as $k => $v)
	    {
	        if($v['last'] == 1)
	        {
	            $arr = $v;
	            break;
	        }
	        else 
	        {
	            $nowCount = $index->getNowCardNum();
	            $haveNum = $allCount / ($allCount * $v['card_chance'] / 100);
	            $shouldNum = floor(($nowCount + 1) / $haveNum);
	            if($v['card_user'] < $shouldNum && $v['card_user'] < $v['card_num'])
	            {
	                $arr = $v;
	                break;
	            }
	        }
	    }
	    return $arr;
	}
	
	public function setStatus()
	{
	    $res = array('error' => 1, 'content' => '系统繁忙');
	    $index = Model('index');
	    if($_SESSION['wxuser'])
	    {
	        if($_SESSION['wxuser']['id'] != $_POST['wxid'] || $_SESSION['wxuser']['openid'] != $_POST['openid'])
	        {
	            $this->showMsg($res);
	        }
	        $id = $index->updateStatus($_SESSION['wxuser']['id'], $_SESSION['wxuser']['openid']);
	        if($id)
	        {
	            $res = array('error' => 0, 'content' => '');
	        }
	    }
	    else 
	    {
	        $res['content'] = '信息错误,请重新进入该页面';
	    }
	    $this->showMsg($res);
	}
	
	public function setSession()
	{
	    $openid = 'oLUqRjqun6z3tK3m46Z5GJ3Cb4nM';
	    $index = Model('index');
	    $user = $index->getUserInfo($openid);
	    $_SESSION['wxuser'] = $user;
	    $this->index();
	}
	
	public function clear()
	{
	    unset($_SESSION['wxuser']);
	}
	
}